<?php

include_once("library/facil3/navigation/http/HttpResult.class.php");

//include_once("library/forum/modules/contact/dao/ContactDAO.class.php");
//include_once("library/forum/modules/contact/vo/ContactVO.class.php");

include_once("facil3/core/controller/interface/HTTPControllerInterface.class.php");
include_once(DataHandler::removeDobleBars(Config::getFolderView("HttpRoot.class.php")));

include_once "library/facil3/core/controller/mail/MailInfoPostVO.php" ;
include_once "library/facil3/utils/Mail.class.php";
//para retorno do envio e cadasro de mail
include_once "library/facil3/core/vo/ReturnResultVO.class.php";

/**
 * @author 		: Mauricio Amorim
 * @date		: 1/01/2011
 * @version		: 1.0 (1.0 seria se a pessoa tivesse testado e funcionou)
 * @desc		: Controller de contato onde extende a controller mail
 */

/**
 * @author Renato
 * @date		: 19/01/2011
 * @version		: 1.0
 * @desc		: 
 */
class Contact implements HTTPControllerInterface  {
	//configurações e dados a serem a serem enviados no email
	public $MailInfoPost;
	
	public $arrayVariable;
	public $arrayRestFolder;
	
	public function __construct($arrayRestFolder = NULL){
		if($arrayRestFolder != NULL){
			$this->arrayVariable   = Navigation::getVariableArraySlug($arrayRestFolder);
            $this->arrayRestFolder = $arrayRestFolder;
        }
	}
	
    /*
     * naum faz nada pq é só para mostrar o html
     */
    public function init(){
		//iniciando o retorno padrao em http result
		$returnResult = new HttpResult();
		//iniciando o resultado para o html
		$retornoDaPaginaHTML = new HttpRoot();
		$returnResult->setSuccess(TRUE);
		//salvando o objeto de resultado de html no retorno
		$returnResult->setHttpContentResult($retornoDaPaginaHTML);
		//Debug::print_r($returnResult);
		return $returnResult;
    }
    
    /*
     * rescebe os dados por post e envia e manda por email atravez da ccontroller FacilMail
     * @return ReturnResultVO
     */
    public function mail(){
    	//inicia um retorno de ReturnResultVO
    	$ReturnResultVO = new ReturnResultVO();
		
    	//inicia a vo para cadastrar os dados no banco
    	$ContactVO = new ContactVO($_POST);
		
		//verifica os campos a serem salvos no Banco obrigatorios
		$ReturnResultVO = $ContactVO->validate();
    	//se nao validadar ele retorna ReturnResultVO em json;
		if(!$ReturnResultVO->success){
    		echo utf8_encode($ReturnResultVO->toJson());
    		exit();
    	}
    	$ContactVO->commit();
    	//agora envia por email realmente
    	$message = file_get_contents(Config::getFolderView("mail/template_contact.html"));
    	/**
    	
    	 */
    	$message = str_replace(
    							array("php(subject)", 			"php(name)", 			"php(company)", 			"php(email)",			"php(telephone)",		"php(comment)"), 
    							array($ContactVO->getSubject(), $ContactVO->getName(), 	$ContactVO->getCompany(),	$ContactVO->getEmail(), $ContactVO->getPhone(), $ContactVO->getMessage()), 
    							$message);
    	
    	$ContactVO->getMessage();
    	$Mail = new Mail();
    	//preciso por no config o email de destino
    	$Mail->setTo("info@seminars.com.br", "daniel");
    	$Mail->setFrom($ContactVO->getEmail(), $ContactVO->getName());
    	$Mail->setMessage($message);
    	$Mail->setSubject("mensagem enviada pelo forum internacional:".$ContactVO->getSubject());
    	//silence is gold :) 
    	@$Mail->send();
    	$ReturnResultVO->addMessage(Translation::text("Message sent successfully."));
    	echo utf8_encode($ReturnResultVO->toJson());
    	exit();
    }
}